var express = require('express')
var router = express.Router();
var {classifyModel} = require('../model/classify')
var {goodModel} = require('../model/goods')

// 无限极分类
router.get('/getClassify',async(req,res)=>{
    // 可添加字段
    let classify = await classifyModel.find().lean()
    // 不可添加字段
    let all = await classifyModel.find()
    let data =[]
    let obj ={}
    classify.forEach(item => {
        obj[item._id] = item
    });
    classify.forEach(item=>{
        if(!item['pid']){
            data.push(item)
        }else{
            if(!obj[item['pid']]['children']){
                obj[item['pid']]['children'] = []
            }
            obj[item['pid']]['children'].push(item)
        }
    })
    res.send({
        code:200,
        data,
        // all
    })
})

// 获取单个三级类的数据
router.get('/getItem',async(req,res)=>{
    let id = req.query.id
    let data = await goodModel.find({classid:id})
    res.send({code:200,data})
})

// 单个商品的详情
router.get('/getgood',async(req,res)=>{
    let id = req.query.id
    let data = await goodModel.find({_id:id})
    res.send({code:200,data})

})

// 仅是一级分类
router.get('/getOne',async(req,res)=>{
    let data = await classifyModel.find()
    let list=[]
    data.forEach(item=>{
        if(!item.pid){
            list.push(item)
        }
    })
    res.send({code:200,data:list})
})

module.exports = router;